

#include "StartPromptView.h"



Scene *StartPromptView::createScene() {
	auto scene = Scene::create();
	auto layer = StartPromptView::create();
	scene->addChild(layer);
	return scene;
}

bool StartPromptView::init() {
	if (!Layer::init()) {
		return false;
	}

	win_height = Director::getInstance()->getWinSize().height;
	win_width = Director::getInstance()->getWinSize().width;

	UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile(
				"StartPromptScreen_1.ExportJson");
	   UI->setAnchorPoint(Vec2::ZERO);
		UI->setPosition(Point(0, win_height - UI->getContentSize().height));
		this->addChild(UI);


	    promptFrame = (Widget*)Helper::seekWidgetByName(UI, "prompt_frame");
		promptFrame->setAnchorPoint(Vec2::ZERO);

		Color4B c;
		c.a = 255;
		c.r = 140;
		c.g = 91;
		c.b = 0;
		promptTitle = Label::create("", "Arial", 18);
		promptTitle->setTextColor(c);
		promptTitle->setPosition(Point(promptFrame->getContentSize().width/2, promptFrame->getContentSize().height/2+20));
	        promptFrame->addChild(promptTitle);

	        promptHint = Label::create("", "Arial", 14);
	        promptHint->setTextColor(c);
	        promptHint->setPosition(Point(promptFrame->getContentSize().width/2, promptFrame->getContentSize().height/2-10));
	        	        promptFrame->addChild(promptHint);


	return true;
}

void StartPromptView::dismiss(){
	_callback(this);
    this->removeFromParent();
}

std::string StartPromptView::getTitle(int level){

	return "Level  " + Value(level).asString();

}

void StartPromptView::setImg(int level){
	auto cache = SpriteFrameCache::getInstance();
		cache->addSpriteFramesWithFile("Prompt.plist");
		cache->addSpriteFramesWithFile("Unit.plist");
	//	cache->addSpriteFramesWithFile("Monsters.plist");
//		__String *fileName = __String::createWithFormat("Monster%d.png",level);
		Sprite* sprite = NULL;

		switch(level){
		case 1:
	sprite = Sprite::createWithSpriteFrameName("PromptIcon1.png");
	addImgChild(sprite);
//	sprite = Sprite::createWithSpriteFrameName(fileName->getCString());
        break;
		case 2:
			sprite = Sprite::createWithSpriteFrameName("PromptIcon4.png");
			addImgChild(sprite);
			        break;
		case 3:
			sprite = Sprite::createWithSpriteFrameName("Fire.png");
			addImgChild(sprite);
						        break;

		case 4:
					sprite = Sprite::createWithSpriteFrameName("Arrow.png");
								       addImgChild(sprite);
								        break;
		default:
			break;

		}
}

void StartPromptView::addImgChild(Sprite* s){
	s->setPosition(
									    		   promptFrame->getContentSize().width/2 - promptHint->getContentSize().width/2  - 30
									    		   , promptFrame->getContentSize().height/2-10);
									        promptFrame->addChild(s);
}


std::string StartPromptView::getHint(int level){

	__String *hint = __String::createWithFormat("StartpromptHint_L%d",level);

	return LocalizedStringGetter::getStringByKey(hint->getCString());


}

void StartPromptView::setLevel(int level){
	currentLevel = level;
}

void StartPromptView::show(){
	promptTitle->setString(getTitle(currentLevel));
	promptHint->setString(getHint(currentLevel));
	setImg(currentLevel);

    MoveTo* moveTo = MoveTo::create(0.3f, Point(0,
    		    UI->getContentSize().height -   win_height/2 - promptFrame->getContentSize().height/2));

    		JumpBy* jumpBy = JumpBy::create(0.5f, Point(0, 0), 15, 1);

    		MoveTo* moveStay = MoveTo::create(1.5f, Point(0,
    				 UI->getContentSize().height -   win_height/2 - promptFrame->getContentSize().height/2));

    		MoveTo* moveBack = MoveTo::create(0.3f, Point(0,
    				UI->getContentSize().height	-	promptFrame->getContentSize().height));

    		auto callbackFunc = [&](){
    	       dismiss();
    		    };
    		    CallFunc* callFunc = CallFunc::create(callbackFunc);


    		Action* action = Sequence::create(moveTo, jumpBy, moveStay, moveBack, callFunc, NULL);
    		promptFrame->runAction(action);
}

void StartPromptView::setCallback(const StartPromptViewCallback& callback)
{
    _callback = callback;
}


